package com.supplychain.scm.core.oss;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.supplychain.scm.core.basis.constant.StringPool;
import com.supplychain.scm.core.oss.model.DbyFile;
import com.supplychain.scm.core.oss.model.UpFile;
import com.supplychain.scm.core.oss.model.UpInputStream;
import com.supplychain.scm.core.oss.model.UpToken;
import com.supplychain.scm.core.common.validator.ValidationUtils;
import lombok.AllArgsConstructor;
import org.dromara.dynamictp.common.util.StringUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 阿里云oss操作
 *
 * @author liyichuan
 * create on 2022-08-08
 */
@AllArgsConstructor
public class AliOssService {

    private AliOssTemplate aliOssTemplate;

    /**
     * 上传文件
     *
     * @param upFile
     * @return
     */
    public DbyFile putFile(UpFile upFile) {
        ValidationUtils.validateEntity(upFile);
        MultipartFile file = upFile.getFile();
        String mchId = upFile.getMchId();
        String originalFilename = file.getOriginalFilename();
        // 产品 + 商户
        String prefix = upFile.getProductInfo().getValue();

        if (StringUtil.isNotEmpty(mchId)) {
            prefix += StrPool.SLASH + mchId;
        }

        return aliOssTemplate.putFile(prefix, originalFilename, file);
    }

    /**
     * 上传文件流
     *
     * @param inputStream
     * @return
     */
    public DbyFile putFile(UpInputStream inputStream) {
        ValidationUtils.validateEntity(inputStream);
        InputStream is = inputStream.getInputStream();
        String mchId = inputStream.getMchId();

        String fileName = inputStream.getFileName();
        // 产品 + 商户
        String prefix = inputStream.getProductInfo().getValue();

        if (StringUtil.isNotEmpty(mchId)) {
            prefix += StringPool.SLASH + mchId;
        }

        return aliOssTemplate.putFile(prefix, fileName, is);
    }

    /**
     * 上传文件流 固定文件名覆盖方式
     *
     * @param inputStream
     * @return
     */
    public DbyFile putFileCover(UpInputStream inputStream) {
        ValidationUtils.validateEntity(inputStream);
        InputStream is = inputStream.getInputStream();
        String mchId = inputStream.getMchId();

        String fileName = inputStream.getFileName();
        // 产品 + 商户
        String prefix = inputStream.getProductInfo().getValue();

        if (StringUtil.isNotEmpty(mchId)) {
            prefix += StringPool.SLASH + mchId;
        }
        return aliOssTemplate.putFileCover(prefix, fileName, is);
    }

    /**
     * 上传文件流 固定文件名覆盖方式
     *
     * @param inputStreamList
     * @return
     */
    public List<DbyFile> putFileCover(List<UpInputStream> inputStreamList, boolean isCover) {
        List<DbyFile> dbyFileList = new ArrayList<>();
        if (CollUtil.isNotEmpty(inputStreamList)) {
            inputStreamList.forEach(e -> {
                if (isCover) {
                    dbyFileList.add(putFileCover(e));
                } else {
                    dbyFileList.add(putFile(e));
                }
            });
        }
        return dbyFileList;
    }

    /**
     * 前端直传 获取token
     *
     * @param upToken
     * @return
     */
    public Map<String, String> getUpToken(UpToken upToken) {
        ValidationUtils.validateEntity(upToken);
        return aliOssTemplate.getUploadTokenMap(null, getKey(upToken));
    }


    /**
     * 私有上传Token
     *
     * @param upToken
     * @return
     */
    public Map<String, String> getPrivateUpToken(UpToken upToken) {
        ValidationUtils.validateEntity(upToken);
        return aliOssTemplate.getPrivateUpTokenMap(getKey(upToken));
    }

    /**
     * 获取签名链接
     *
     * @param key
     * @return
     */
    public URL generatePresignedUrl(String key) {
        return aliOssTemplate.generatePresignedUrl(key);
    }

    /**
     * 静态资源上传
     *
     * @param objectName
     * @return
     */
    public Map<String, String> getStaticUpToken(String objectName) {
        return aliOssTemplate.getUploadTokenMap(null, objectName);
    }

    private String getKey(UpToken upToken) {
        String fileName = upToken.getFileName();
        String suffix = fileName.substring(fileName.lastIndexOf(StringPool.DOT));
        String dir = upToken.getProductInfo().getValue() + (StrUtil.isNotEmpty(upToken.getMchId()) ? StringPool.SLASH + upToken.getMchId() : "");
        return dir + StringPool.SLASH + DateUtil.today().replace(StringPool.DASH, StringPool.SLASH) + StringPool.SLASH + IdUtil.fastSimpleUUID()  + suffix;
    }
}
